<?php 
	
namespace app\font\controller;
use think\Db; // 引用DB类
use think\Request;
use think\Session;

class Font
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index(){
    	
    }
    
    public function getfont($page){
    	$pagesize = 8;
    	
    	$a	= Db::table("renwu")
		-> where("renwu_isTake",0)
    	->page($page,$pagesize)
    	-> select();
    	$count = count($a);
    	if($a){
    		return json(["data"=>$a]);
    	}
    	
    	
    	
    }
	public function getfontdetail($id){
    	
    	$a	= Db::table("renwu")
    	-> where("renwu_id",$id)
    	-> select();
    	
    	return json(["data"=>$a]);
    	
    }
	//接单成功
	public function successorder($id,$user_name){
		
		$select	= Db::table("user_font")
    		   -> where("user_name",$user_name)
    		   -> column("user_is_online");
		
		$renwu_id	= Db::table("renwu_font")
    		   -> where("renwu_id",$id)
    		   -> column("renwu_id");
		if($renwu_id){
			return '任务已被其他人接走,请选择其他任务';
		}
		$shangjia	= Db::table("renwu")
    		   -> where("renwu_id",$id)
    		   -> column("renwu_good_shop");
		
		
		
		$times	= Db::query("
		SELECT
			TIMESTAMPDIFF(day,df.timestamp,now()) as times
		FROM
			dingdan_font df
		left join 
			renwu_font rf
		on
			rf.renwu_id=df.renwu_id
		WHERE
			df.user_name = '".$user_name."'
			and rf.renwu_good_shop='".$shangjia[0]."'
			ORDER BY df.timestamp desc
			LIMIT	1");
		//return json($times[0]['times']);
		if($times){
			if ($times[0]['times']<7){
				return '您在此店铺7天内有接单信息,不允许接单。请选择其他任务';
			}
		}
		
		
		
		if($select){
			
			if($select[0]==1){
				
				$data = ['renwu_isTake' => 1,'font_name' => $user_name];
				Db::table('renwu')
					->where('renwu_id',$id)
					->update($data);
					
					
				$a	= Db::table("renwu")
				-> where("renwu_id",$id)
				-> select();
				
				Db::table('renwu_font')
				->data($a[0])
				->insert();
				
				return '接单成功';
			}
			else{
				return '登录超时请重新登录';
			}
		}
    	else{
			return '您还未登录，请先前往登录';
		}
    }
	
	//任务完成后提交任务待商家审核
	public function Submit($id){
		
		$data = ['renwu_isDo' => 1];
		Db::table('renwu')
			->where('renwu_id',$id)
			->update($data);
			
		$data = ['renwu_isDo' => 1];
		Db::table('renwu_font')
			->where('renwu_id',$id)
			->update($data);
		
		
		return json(["msg"=>"提交成功","code"=>200]);
		
    	
    }
	public function fontDel($id){
		
		$data = ['renwu_isDo' => 0,'renwu_isTake'=>0 ,'font_name'=>''];
		
		Db::table('renwu')
			->where('renwu_id',$id)
			->update($data);
			
		
		
		Db::table('renwu_font')->where('renwu_id',$id)->delete();
		Db::table('dingdan_font')->where('renwu_id',$id)->delete();
		return json(["msg"=>"提交成功","code"=>200]);
		
    	
    }
	
    //获取店铺旺旺号=》下拉框
    public function select($user){
    	$select	= Db::table("shop")
    		   -> where("user_name",$user)
    		   -> column("shop_name");
    	
    	return json(["data"=>$select]);
    }
    
    //发布任务活动
    public function addRenwu(){
    	$renwu_shop_type		= request()->post("renwu_shop_type");
    	$renwu_xiadan			= request()->post("renwu_xiadan");
    	$renwu_good_shop		= request()->post("renwu_good_shop");
    	$renwu_url				= request()->post("renwu_url");
    	$renwu_good_titles		= request()->post("renwu_good_titles");
    	$renwu_img_url			= request()->post("renwu_img_url");
    	$renwu_good_price		= request()->post("renwu_good_price");
    	$renwu_good_payPrice	= request()->post("renwu_good_payPrice");
    	$renwu_good_searchWord	= request()->post("renwu_good_searchWord");
    	$renwu_good_money		= request()->post("renwu_good_money");
    	$renwu_yaoqiu			= request()->post("renwu_yaoqiu");
    	$user_name				= request()->post("user_name");
    	
    	$renwuAdd				= Db::table("renwu")
    						   -> insert(
    						   		[
    						   			"renwu_shop_type"		=> $renwu_shop_type,
    						   			"renwu_xiadan"			=> $renwu_xiadan,
    						   			"renwu_good_shop"		=> $renwu_good_shop,
    						   			"renwu_url"				=> $renwu_url,
    						   			"renwu_good_titles"		=> $renwu_good_titles,
    						   			"renwu_img_url"			=> $renwu_img_url,
    						   			"renwu_good_price"		=> $renwu_good_price,
    						   			"renwu_good_payPrice"	=> $renwu_good_payPrice,
    						   			"renwu_good_searchWord"	=> $renwu_good_searchWord,
    						   			"renwu_good_money"		=> $renwu_good_money,
    						   			"renwu_yaoqiu"			=> $renwu_yaoqiu,
    						   			"renwu_addTime"			=> Db::raw("now()"),
    						   			"user_name"				=> $user_name,
    						   			"renwu_isTake"			=> 0,
    						   			"renwu_isDo"			=> 0,
    						   			"renwu_isCheck"			=> 0
    						   		]);
    }
    
    //编辑任务
    public function editRenwu(){
    	$renwu_shop_type		= request()->post("renwu_shop_type");
    	$renwu_xiadan			= request()->post("renwu_xiadan");
    	$renwu_good_shop		= request()->post("renwu_good_shop");
    	$renwu_url				= request()->post("renwu_url");
    	$renwu_good_titles		= request()->post("renwu_good_titles");
    	$renwu_img_url			= request()->post("renwu_img_url");
    	$renwu_good_price		= request()->post("renwu_good_price");
    	$renwu_good_payPrice	= request()->post("renwu_good_payPrice");
    	$renwu_good_searchWord	= request()->post("renwu_good_searchWord");
    	$renwu_good_money		= request()->post("renwu_good_money");
    	$renwu_yaoqiu			= request()->post("renwu_yaoqiu");
    	$renwu_id				= request()->post("renwu_id");
    	
    	$renwuAdd				= Db::table("renwu")
    						   -> where("renwu_id",$renwu_id)
    						   -> update(
    						   		[
    						   			"renwu_shop_type"		=> $renwu_shop_type,
    						   			"renwu_xiadan"			=> $renwu_xiadan,
    						   			"renwu_good_shop"		=> $renwu_good_shop,
    						   			"renwu_url"				=> $renwu_url,
    						   			"renwu_good_titles"		=> $renwu_good_titles,
    						   			"renwu_img_url"			=> $renwu_img_url,
    						   			"renwu_good_price"		=> $renwu_good_price,
    						   			"renwu_good_payPrice"	=> $renwu_good_payPrice,
    						   			"renwu_good_searchWord"	=> $renwu_good_searchWord,
    						   			"renwu_good_money"		=> $renwu_good_money,
    						   			"renwu_yaoqiu"			=> $renwu_yaoqiu,
    						   			"renwu_addTime"			=> Db::raw("now()")
    						   		]);
    }
    
    //删除单个任务
    public function delRenwu($id){
    	$isDel	= Db::table("renwu")
    		   -> where("renwu_id",$id)
    		   -> delete();
    		   
    	if($isDel){
    		return json(["msg"=>"删除成功","code"=>200]);
    	}else{
    		return json(["msg"=>"请重试","code"=>500]);
    	}
    }
    
    //批量删除
    public function delMoreRenwu($renwu_id){
    	foreach($renwu_id as $renwu_id){
			$del	= Db::table('renwu')
			   	   -> where('renwu_id', $renwu_id)
			   	   -> delete();
		}
    }
    
    //搜索任务
    public function renwuSearch(){
    	$key			= request()->post("key");
    	$user_name	= request()->post("user_name");
		$isRenwu		= Db::table("renwu")
						-> where("user_name",$user_name)
						-> where("renwu_good_shop|renwu_good_searchWord|renwu_good_money","like","%".$key."%")
				   		-> order("renwu_addTime desc")
				   		-> select();
		$count			= count($isRenwu);		   
		return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$isRenwu]);
    }
    
    //返回任务列表（直接自己接的单并且商家未确认才可显示出来）
    public function renwuList($id){
    	$renwuList	= Db::table("renwu_font")
    			   -> where("font_name",$id)
				   -> where("renwu_isCheck is null or renwu_isCheck=0")
    			   -> order("renwu_addtime desc")
    			   -> select();
				
    	$count		= count($renwuList);
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$renwuList]);
    } 
	public function renwuyiwancheng($id){
    	$renwuList	= Db::table("renwu_font")
    			   -> where("font_name",$id)
				   -> where("renwu_isCheck=1")
    			   -> order("renwu_addtime desc")
    			   -> select();
				
    	$count		= count($renwuList);
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$renwuList]);
    } 
	//返回任务列表（直接自己接的单并且商家未确认才可显示出来）
	
    public function getpersonalinformation($user_name){
    	$information	= Db::query("
							SELECT USER
	.user_name,
	USER.user_phone,
	USER.user_qq,
	USER.user_wx,
	bal.font_money,
	bal.tixian_money 
FROM
	user_font
	USER LEFT JOIN balance_font bal ON bal.user_name = USER.user_name 
WHERE
	USER.user_name = '".$user_name."'");
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","data"=>$information]);
    }
	
	public function getkahaoinformation($user_name){
    	$information	= Db::query("
							SELECT name
								,yinhangkahao
								,kaihuhang 
							FROM
								user_font
								
							WHERE
								user_name = '".$user_name."'");
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","data"=>$information]);
    }
	
	public function xiugaitijiao($user_name,$qq,$wx){
		
		$data = ['user_qq' => $qq,'user_wx'=>$wx ];
		
		
		Db::table('user_font')
			->where('user_name',$user_name)
			->update($data);
			
		
		return json(["msg"=>"提交修改成功","code"=>200]);
		
    	
    }
	
	
	public function tianjiakahao($user_name,$name,$yinhangkahao,$kaihuhang)
	{
		
		$data = ['name' => $name,'kaihuhang'=>$kaihuhang,'yinhangkahao'=>$yinhangkahao];
		
		Db::table('user_font')
			->where('user_name',$user_name)
			->update($data);
			
		
		return json(["msg"=>"添加银行卡成功","code"=>200]);
		
    	
    }
	public function tixian($user_name,$name,$yinhangkahao,$kaihuhang,$tixian_jin_er)
	{
		
		$select	= Db::table("balance_font")
    		   -> where("user_name",$user_name)
    		   -> column("font_money");
		if($select){
			if($select[0]<$tixian_jin_er){
				return json(["msg"=>"提现失败,超出佣金所得","code"=>200]);
			}
			else{
				
				Db::table('balance_font')
					->where('user_name', $user_name)
					->setInc('tixian_money', floatval($tixian_jin_er));
				Db::table('balance_font')
					->where('user_name', $user_name)
					->setDec('font_money',floatval($tixian_jin_er));
				
				$data = ['name' => $name,'kaihuhang'=>$kaihuhang,'yinhangkahao'=>$yinhangkahao,'tixian_jin_er'=>$tixian_jin_er,'user_name'=>$user_name,'jindu'=>0];
		
				Db::table('tixian_font')
					->where('user_name',$user_name)
					->insert($data);
				return json(["msg"=>"提现成功,12小时内到账，请注意查收","code"=>200]);
			}
			
		}else{
			return json(["msg"=>"用户不存在请前往登录","code"=>200]);
		}
    }
	
	
	//返回任务列表（直接自己接的单并且商家未确认才可显示出来）
    public function getyitixian($page, $limit,$id){
		
		$tixianListcount	= Db::table("tixian_font")
					-> where("user_name",$id)
					-> where("jindu",1)
					-> select();
    	$tixianList	= Db::table("tixian_font")
    			   -> where("user_name",$id)
				   -> where("jindu",1)
    			   -> order("timestamp desc")
				   ->page($page,$limit)
    			   -> select();
    	$count		= count($tixianListcount);
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$tixianList,]);
    } 
	public function getzhuanzhangzhong($page, $limit,$id){
    	$tixianList	= Db::table("tixian_font")
    			   -> where("user_name",$id)
				   -> where("jindu",0)
    			   -> order("timestamp desc")
				   ->page($page,$limit)
    			   -> select();
				
    	$count		= count($tixianList);
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$tixianList]);
    }
	
	
	//先上传图片，随后将图片地址回传
    public function dingdanImg(){
    	$dingdan_img		= request()->file('file');
		$renwu_id	= request()->get("id");
    	$info 			= $dingdan_img->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'dingdan_img');
    	
    	// 图片保存后会根据日期再生成一个以上传日期命名的文件夹，要将路径存入数据库
    	$date 			= '20'.date('ymd'); 
    	$dingdan_img_url 	=  "http://127.0.0.1:88/static/dingdan_img/".$date."/".$info->getFilename();
    	$isjietu = Db::table("dingdan_font")
            ->where("renwu_id",$renwu_id)
            ->field("dingdan_jietu")
            ->find();
		$font_name = Db::table("renwu_font")
            ->where("renwu_id",$renwu_id)
            ->column("font_name");
		
		//将图片地址存入数据库
        if($isjietu){
			$data = ['dingdan_jietu' => $dingdan_img_url];
		
			Db::table('dingdan_font')
			->where('renwu_id',$renwu_id)
			->update($data);
		}
		else{
			Db::table("dingdan_font")
		   -> insert([
				"renwu_id"		=> $renwu_id,
				"user_name"  =>$font_name[0],
				"dingdan_jietu"  =>$dingdan_img_url,
				"timestamp"	=> Db::raw("now()")
		   ]);
		}
    	
    	
    	if($dingdan_img_url){
    		return json(["url"=>$dingdan_img_url,"code"=>200,"renwu_id"=>$renwu_id]);
    	}else{
    		return json(["url"=>$dingdan_img_url,"code"=>500,"renwu_id"=>$renwu_id]);
    	}
    	
    }
	public function getdingdanjietu($renwu_id,$user_name)
	{
		$select	= Db::table("dingdan_font")
    		   -> where("renwu_id",$renwu_id)
    		   -> column("dingdan_jietu");
		$renwu_good_price	= Db::table("renwu")
    		   -> where("renwu_id",$renwu_id)
    		   -> column("renwu_good_price");
		$yinhangkahao	= Db::table("user_font")
    		   -> where("user_name",$user_name)
    		   -> column("yinhangkahao");
		
		if($select and $yinhangkahao[0]){
			return json(["msg"=>"获取成功","code"=>200,"dingdan_jietu"=>$select[0],"renwu_good_price"=>$renwu_good_price]);
		}if(!$select){
			return json(["msg"=>"您还未上传订单截图","code"=>500,"dingdan_jietu"=>'0',"renwu_good_price"=>'0']);
		}
		if(!$yinhangkahao[0]){
			return json(["msg"=>"您还未上传银行卡信息,请前往提现处添加","code"=>507,"dingdan_jietu"=>'0',"renwu_good_price"=>'0']);
		}
    }
	public function tijiaocailiao($renwu_id,$user_name,$renwu_good_price,$shifu_jine,$dingdan_jietu,$dingdanhao)
	{
		$information	= Db::query("
			SELECT name
				,yinhangkahao
				,kaihuhang 
			FROM
				user_font
				
			WHERE
				user_name = '".$user_name."'");
		
		
		$data = ['renwu_good_price'=>$renwu_good_price,'shifu_jine'=>$shifu_jine,'name'=>$information[0]['name'],'yinhangkahao'=>$information[0]['yinhangkahao'],'kaihuhang'=>$information[0]['kaihuhang'],'dingdanhao'=>$dingdanhao];
		
			Db::table('dingdan_font')
			->where('renwu_id',$renwu_id)
			->where('user_name',$user_name)
			->update($data);
		return json(["msg"=>"提交成功","code"=>200]);
    }
	
	public function getshangjiashenhe($renwu_id)
	{
		
		$check_reason	= Db::table("renwu_font")
    		   -> where("renwu_id",$renwu_id)
    		   -> column("check_reason");
		if($check_reason[0]!=null && $check_reason){
			return json(["msg"=>"获取成功","code"=>200,"check_reason"=>$check_reason[0]]);
			
		}
		else{
			
			return json(["msg"=>"获取成功","code"=>200,"check_reason"=>'商家未审核']);
		}
    }
}
